<!-- 添加学生 -->
<template>
  <section class="add">
    <el-form ref="form" :model="form" :rules="addFormRules" label-width="80px">
      <el-form-item label="姓名"  prop="studentName">
        <el-input v-model="form.studentName"></el-input>
      </el-form-item>
      <el-form-item label="性别">
            <el-radio-group v-model="form.sex">
              <el-radio label="男" size="large">男</el-radio>
              <el-radio label="女" size="large">女</el-radio>
              <!-- <el-radio label="GAI" size="large">GAI</el-radio> -->
            </el-radio-group>
            <!-- <el-input v-model="form.sex"></el-input> -->
          </el-form-item>
      <el-form-item label="学院">
        <el-select v-model="form.institute"  :placeholder="form.institute" size="large" @change="ZYchange(form.institute)">
                <el-option
                  v-for="item in xyForm"
                  :key="item"
                  :label="item"
                  :value="item"
                />
          </el-select>
      </el-form-item>
      <el-form-item label="所属专业">
            <!-- <el-input v-model="form.type"></el-input> -->
            <el-select v-model="form.major"  :placeholder="form.major" size="large" @focus="checkXY(form.institute)">
                <el-option
                  v-for="item in zyForm"
                  :key="item"
                  :label="item"
                  :value="item"
                />
          </el-select>
          </el-form-item>
      <el-form-item label="年级">
        <el-input v-model="form.grade"></el-input>
      </el-form-item>
      <el-form-item label="班级">
        <el-input v-model="form.clazz"></el-input>
      </el-form-item>
      <el-form-item label="电话号码"   prop="tel">
        <el-input v-model="form.tel"></el-input>
      </el-form-item>
      <el-form-item label="身份证号"   prop="cardId">
        <el-input v-model="form.cardId"></el-input>
      </el-form-item>
      <el-form-item label="邮箱"   prop="email">
        <el-input v-model="form.email"></el-input>
      </el-form-item>
      <el-form-item label="密码"   prop="pwd">
        <el-input v-model="form.pwd"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit()">立即创建</el-button>
        <el-button type="text" @click="cancel()">取消</el-button>
      </el-form-item>
    </el-form>
  </section>
</template>

<script>
export default {
  data() {
    
    const checkEmail = (rule, value, cb) => {
      const regEmail = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/
      if (regEmail.test(value)) {
        return cb()
      }
      cb(new Error('请输入正确的邮箱'))
    };
    const checkMobile = (rule, value, cb) => {
      const regMobile = /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/
      if (regMobile.test(value)) {
        return cb()
      }
      cb(new Error('请输入正确的手机号'))
    };
    const checkSid = (rule, value, cb) => {
      const regSid = /^[1-9]\d{5}(18|19|20|(3\d))\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/
      if (regSid.test(value)) {
        return cb()
      }
      cb(new Error('请输入正确的身份证号'))
    };
    return {
      form: { //表单数据初始化
        studentName: null,
        grade: null,
        major: null,
        clazz: null,
        institute: null,
        tel: null,
        email: null,
        pwd: null,
        cardId: null,
        sex: null,
        role: 2
      },
       // 职称表单
       zyForm:{},
       xyForm:{},

            // 验证规则
        addFormRules:{
        teacherName:[
           { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 1, max: 8, message: "长度在 1 到 8 个字符", trigger: "blur" }
        ],
        pwd:[
           { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 8, message: "长度在 6 到 8 个字符", trigger: "blur" }
        ],
       
        
        email: [
          {
            required: true,
            message: '请输入邮箱',
            trigger: 'blur'
          },
          {
            // 通过validator 进行自定义校验规则
            validator: checkEmail,
            trigger: 'blur'
          }
        ],
        // /^[1-9]\d{5}(18|19|20|(3\d))\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
        cardId: [
          {
            required: true,
            message: '请输入身份证号',
            trigger: 'blur'
          },
          {
            // 通过validator 进行自定义校验规则
            validator: checkSid,
            trigger: 'blur'
          }
        ],
        tel: [
          {
            required: true,
            message: '请输入手机号',
            trigger: 'blur'
          },
          {
            validator: checkMobile,
            trigger: 'blur'
          },
        ],
      },
    };
  },
  created() {
    this.zyForm={};
    // this.getzy();
    this.getxy();
  },
  methods: {
    onSubmit() { //数据提交
 // this.$refs.起的名字
   //validate是组件提供的一个方法了里边是一个回调函数
   //只有一个参数，它代表表单验证是否通过，通过为true
   this.$refs.form.validate(callback => {
        if (callback) {
          this.$axios({
        url: '/api/student',
        method: 'post',
        data: {
          ...this.form
        }
      }).then(res => {
        if(res.data.code == 200) {
          this.$message({
            message: '数据添加成功',
            type: 'success'
          })
          this.$router.push({path: '/studentManage'})
        }
      })
        } else {
          this.$message.success("数据校验不通过，操作失败！！！");
        }
      }
     )

    },
  // 选了学院自动更新专业数组
  ZYchange(XY){
          console.log("xyxyxyx==="+XY)
          this.getzy1(XY);
      },
         // 没选择学院不可以选择专业提示，
         checkXY(XY){
          console.log("xyxyxyx==="+XY)
          if (XY==null||XY=="") {
            this.$message.success(" 没选择学院不可以选择专业！！");
          }
      },
           // 专业列表
           getzy() {
      //分页查询所有职称列表
      this.$axios(`/api/zy`).then(res => {
        this.zyForm = res.data.data;
        // console.log("sssss"+this.form );
      }).catch(error => {});
    },
      // 学院专业列表
        getzy1(XY) {
          console.log("xyxyxyx=2222=="+XY)
      //分页查询所有职称列表
      this.$axios(`/api/zy/${XY}`).then(res => {
        this.zyForm = res.data.data;
        // console.log("sssss"+this.form );
      }).catch(error => {});
    },
           // 学院列表
           getxy() {
      //分页查询所有职称列表
      this.$axios(`/api/xys`).then(res => {
        this.xyForm = res.data.data;
        // console.log("sssss"+this.form );
      }).catch(error => {});
    },
    cancel() { //取消按钮
      this.form = {}
    },
    
  }
};
</script>
<style lang="less" scoped>
.add {
  padding: 0px 40px;
  width: 400px;
}
</style>

